<template>
  <el-aside width="220px" class="sidebar">
    <div class="logo">
      <img src="src\assets\logo.jpg" style="width: 60px; height: 60px;"   salt="测盟汇">
      </div>
    <el-menu :default-active="activeMenu" class="el-menu-vertical-demo" router background-color="#fff"
      text-color="#001529" active-text-color="#409EFF">
      <el-menu-item v-if="showUserMenuItem" index="/user">
        <el-icon><User /></el-icon>
        <span>用户管理</span>
      </el-menu-item>
      <el-menu-item v-if="showUserMenuItem" index="/tenant">
        <el-icon><OfficeBuilding /></el-icon>
        <span>租户管理</span>
      </el-menu-item>
      <el-menu-item v-if="showUserMenuItem" index="/department">
        <el-icon><Suitcase /></el-icon>
        <span>部门管理</span>
      </el-menu-item>
      <el-menu-item index="/news">
        <el-icon><Message /></el-icon>
        <span>咨询管理</span>
      </el-menu-item>
      <el-menu-item index="/course">
        <el-icon><Notebook /></el-icon>
        <span>课程管理</span>
      </el-menu-item>
      <el-menu-item index="/meeting">
      <el-icon><DataLine /></el-icon>
        <span>会议管理</span>
      </el-menu-item>
    </el-menu>
  </el-aside>
</template>

<script setup>
import { useRoute } from 'vue-router'
import { onMounted, ref, watch } from 'vue'
import UserService from '@/services/UserService.js';
import {
  User,
  OfficeBuilding,
  Suitcase,
  Message,
  Notebook,
  DataLine
} from '@element-plus/icons-vue'

const route = useRoute()
const activeMenu = ref(route.path);
const showUserMenuItem = ref(false)

function updateActiveMenu() {
  activeMenu.value = route.path
}

function updateCurrentUser() {
    UserService.loadCurrentUser();
}

watch(
  () => route.path,
  () => {
    updateActiveMenu()
    UserService.loadCurrentUser();
  }
)

function updateShowUserMenuItem() {
    const currentUserStr = localStorage.getItem('currentUser');

    let userRole = null;
    if (currentUserStr) {
        try {
            userRole = JSON.parse(currentUserStr).userType;
        } catch {
            userRole = null;
        }
    }
    if (userRole === 2) {
        showUserMenuItem.value = true;
    } else {
        showUserMenuItem.value = false;
}
}

onMounted(() => {
    updateShowUserMenuItem();
    updateActiveMenu(); 
    updateCurrentUser();
})

</script>

<style>
body,
html,
#app {
  margin: 0;
  padding: 0;
  height: 100%;
  background: #f5f7fa;
}

.sidebar {
  background: #fff;
  color: #001529;
  height: 100vh;
  padding-top: 0;
  border-right: 1px solid #e6e6e6;
  box-shadow: 2px 0 8px #f0f1f2;
}

.logo {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  margin: 32px 0 24px 0;
  color: #409EFF;
  letter-spacing: 4px;
}

.el-menu-vertical-demo {
  border-right: none;
  background: #fff;
}

.el-menu-item {
  font-size: 16px;
  height: 48px;
  line-height: 48px;
}

.el-menu-item .el-icon {
  margin-right: 12px;
  font-size: 20px;
}
</style>